package cn.edu.gues.infosystam.utils;

import java.util.HashMap;
import java.util.Map;

/***
 * 通用返回结果集
 */
public class R {


    /**
     * 状态码：200成功、202失败
     */
    public Integer code;
    /**
     * 响应消息
     */
    public String msg;
    /**
     * 响应的数据
     */
    public Map<String, Object> data = new HashMap<String, Object>();

    private R(){ }

    /**
     * 成功
     * @return
     */
    public static R success(){
        R response = new R();
        response.setCode(200);
        response.setMsg("success");
        return response;
    }

    /**
     * 失败
     * @return
     */
    public static R error(){
        R response = new R();
        response.setCode(202);
        response.setMsg("error");
        return response;
    }

    /**
     * 设置状态码
     * @param code
     * @return
     */
    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    /**
     * 设置响应消息
     * @param msg
     * @return
     */
    public R message(String msg){
        this.setMsg(msg);
        return this;
    }

    /**
     * 追加数据
     * @param key
     * @param value
     * @return
     */
    public R data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    /**
     * 设置响应数据
     * @param map
     * @return
     */
    public R data(Map<String, Object> map){
        this.setData(map);
        return this;
    }

    private void setCode(Integer code) {
        this.code = code;
    }

    private void setMsg(String msg) {
        this.msg = msg;
    }

    private void setData(Map<String, Object> data) {
        this.data = data;
    }

}
